package cn.com.lsy.product.lepao.controller;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.controller.base.BaseController;
import cn.com.lsy.product.lepao.model.mt.entities.OpenDeviceListDto;
import cn.com.lsy.product.lepao.model.mt.entities.OpenDoorListDto;
import cn.com.lsy.product.lepao.model.mt.entities.UserEnterInfoDto;
import cn.com.lsy.product.lepao.model.mt.entities.UserEnterListDto;
import cn.com.lsy.product.lepao.request.OpenDoorListParam;
import cn.com.lsy.product.lepao.request.UserEnterInfoParam;
import cn.com.lsy.product.lepao.response.OpenDeviceListRet;
import cn.com.lsy.product.lepao.response.OpenDoorListRet;
import cn.com.lsy.product.lepao.response.UserEnterInfoRet;
import cn.com.lsy.product.lepao.response.UserEnterListRet;
import cn.com.lsy.product.lepao.response.base.ApiResponseEnum;
import cn.com.lsy.product.lepao.response.base.BaseRet;
import cn.com.lsy.product.lepao.response.base.PageInfo;
import cn.com.lsy.product.lepao.service.IRecordService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import cn.com.lsy.product.lepao.util.DozerMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api(value = "记录管理接口", tags = "记录管理接口")
@RestController
@RequestMapping(value = "/lepao/console/doAuth")
public class RecordController extends BaseController {

    private Logger log = Logger.getLogger(RecordController.class);


    @Autowired
    IRecordService iRecordService;

    @RequestMapping(value = "/openDoorList", method = RequestMethod.POST)
    @ApiOperation(notes = "开门记录列表", httpMethod = "POST", value = "开门记录列表")
    public BaseRet<PageInfo<List<OpenDoorListRet>>> openDoorList(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody OpenDoorListParam param, BindingResult result) {

        BaseRet<PageInfo<List<OpenDoorListRet>>> ret = new BaseRet<PageInfo<List<OpenDoorListRet>>>();

        PageInfo<List<OpenDoorListRet>> pageInfo = new PageInfo<List<OpenDoorListRet>>();

        try {

            checkParam(result);

            String startTime = "";
            String endTime = "";

            if (StringUtils.isNotEmpty(param.getStartTime())) {
                startTime = param.getStartTime() + " 00:00:00";
            }
            if (StringUtils.isNotEmpty(param.getEndTime())) {
                endTime = param.getEndTime() + " 23:00:00";
            }

            int count = iRecordService.selectOpenDoorListByCount(param.getStoreId(), param.getUsername(), startTime, endTime);
            pageInfo.setCount(count);
            List<OpenDoorListDto> list = iRecordService.selectOpenDoorListByPage(param.getStart(), param.getPageSize(), param.getStoreId(), param.getUsername(), startTime, endTime);

            List<OpenDoorListRet> listRet = DozerMapperUtil.mapList(list, OpenDoorListRet.class);


            pageInfo.setRecords(listRet);

            ret.setData(pageInfo);
            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/openDeviceList", method = RequestMethod.POST)
    @ApiOperation(notes = "开器械记录列表", httpMethod = "POST", value = "开器械记录列表")
    public BaseRet<PageInfo<List<OpenDeviceListRet>>> openDeviceList(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody OpenDoorListParam param, BindingResult result) {

        BaseRet<PageInfo<List<OpenDeviceListRet>>> ret = new BaseRet<PageInfo<List<OpenDeviceListRet>>>();

        PageInfo<List<OpenDeviceListRet>> pageInfo = new PageInfo<List<OpenDeviceListRet>>();

        try {

            checkParam(result);

            String startTime = "";
            String endTime = "";

            if (StringUtils.isNotEmpty(param.getStartTime())) {
                startTime = param.getStartTime() + " 00:00:00";
            }
            if (StringUtils.isNotEmpty(param.getEndTime())) {
                endTime = param.getEndTime() + " 23:00:00";
            }

            int count = iRecordService.selectOpenDeviceListByCount(param.getStoreId(), param.getUsername(), startTime, endTime, param.getEquipmentType());
            pageInfo.setCount(count);
            List<OpenDeviceListDto> list = iRecordService.selectOpenDeviceListByPage(param.getStart(), param.getPageSize(), param.getStoreId(), param.getUsername(), startTime, endTime, param.getEquipmentType());

            List<OpenDeviceListRet> listRet = DozerMapperUtil.mapList(list, OpenDeviceListRet.class);


            pageInfo.setRecords(listRet);

            ret.setData(pageInfo);
            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/userEnterList", method = RequestMethod.POST)
    @ApiOperation(notes = "入场记录列表", httpMethod = "POST", value = "入场记录列表")
    public BaseRet<PageInfo<List<UserEnterListRet>>> userEnterList(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody OpenDoorListParam param, BindingResult result) {

        BaseRet<PageInfo<List<UserEnterListRet>>> ret = new BaseRet<PageInfo<List<UserEnterListRet>>>();

        PageInfo<List<UserEnterListRet>> pageInfo = new PageInfo<List<UserEnterListRet>>();

        try {

            checkParam(result);

            String startTime = "";
            String endTime = "";

            if (StringUtils.isNotEmpty(param.getStartTime())) {
                startTime = param.getStartTime() + " 00:00:00";
            }
            if (StringUtils.isNotEmpty(param.getEndTime())) {
                endTime = param.getEndTime() + " 23:00:00";
            }

            int count = iRecordService.selectUserEnterByCount(param.getStoreId(), param.getUsername(), startTime, endTime);
            pageInfo.setCount(count);
            List<UserEnterListDto> list = iRecordService.selectUserEnterByPage(param.getStart(), param.getPageSize(), param.getStoreId(), param.getUsername(), startTime, endTime);

            List<UserEnterListRet> listRet = DozerMapperUtil.mapList(list, UserEnterListRet.class);


            pageInfo.setRecords(listRet);

            ret.setData(pageInfo);
            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/userEnterInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "入场记录详情", httpMethod = "POST", value = "入场记录详情")
    public BaseRet<List<UserEnterInfoRet>> userEnterInfo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UserEnterInfoParam param, BindingResult result) {

        BaseRet<List<UserEnterInfoRet>> ret = new BaseRet<List<UserEnterInfoRet>>();

        try {

            checkParam(result);

            Date time = CommonFunctionUtil.changeTimeDate(param.getDate(), Constant.formatType.date);

            String startTime = CommonFunctionUtil.changeTimeString(time, Constant.formatType.date);

            List<UserEnterInfoDto> list = iRecordService.selectUserEnterInfo(param.getUserId(), startTime + " 00:00:00", startTime + " 23:59:59");

            List<UserEnterInfoRet> listRet = DozerMapperUtil.mapList(list, UserEnterInfoRet.class);

            ret.setData(listRet);
            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }
}
